package com.lecyon.farm.service.impl;

import com.lecyon.farm.base.RestResponse;
import com.lecyon.farm.comment.JwtUtil;
import com.lecyon.farm.common.constant.CommonConstant;
import com.lecyon.farm.service.IUploadTokenService;
import com.lecyon.farm.util.RedisUtils;
import com.lecyon.farm.util.ResultUtil;
import com.lecyon.farm.util.StringHelper;
import com.lecyon.farm.vo.query.QueryUploadTokenVo;
import com.lecyon.farm.vo.request.ConstantQiniu;
import com.lecyon.farm.vo.view.UploadTokenVo;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author Yao Zheng
 * @Date 2019/10/12 17:18
 */
@Service
@Slf4j
public class UploadTokenServiceImpl implements IUploadTokenService {

    private ConstantQiniu constantQiniu;

    private RedisUtils redisUtils;

    /**
     * 获取七牛上传的uploadToken
     *
     * @return
     */
    @Override
    public String getUploadToken() {
        String uploadToken;
        Auth auth = Auth.create(constantQiniu.getAccessKey(), constantQiniu.getSecretKey());
        uploadToken = auth.uploadToken(constantQiniu.getBucket());
        return uploadToken;
    }

    /**
     * 获取七牛上传的uploadToken
     *
     * @return
     */
    @Override
    public RestResponse<UploadTokenVo> requestUploadToken(QueryUploadTokenVo vo) {
        RestResponse<UploadTokenVo> result = new RestResponse<>();
        boolean flag = verifyJwtToken(vo.getJwt());
        String uploadTokenStr;
        UploadTokenVo resultVo = new UploadTokenVo();
        if (!flag) {
            log.warn("Get uploadTokenVo unsuccessful. QueryUploadTokenVo : {}", vo.toString());
            return ResultUtil.error(result, CommonConstant.EX_CLIENT_INVALID, "客户端token异常");
        }
        uploadTokenStr = getUploadToken();
        resultVo.setUploadToken(uploadTokenStr);
        log.info("Get uploadTokenVo success. token : {}", resultVo.toString());
        return ResultUtil.success(result, resultVo);
    }

    /**
     * 认证 jwtToken
     *
     * @return
     */
    private boolean verifyJwtToken(String jwtToken) {
        if (StringHelper.isEmpty(jwtToken)) {
            return false;
        }
        // 解密获得username，用于和数据库进行对比
        String username = JwtUtil.getUsername(jwtToken);
        String sysUserId = JwtUtil.getSysUserId(jwtToken);
        if (StringHelper.isEmpty(username) || StringHelper.isEmpty(sysUserId)) {
            return false;
        }
        Map<String, Object> redisHash;
        String account = null;
        if (redisUtils.hasKey(sysUserId)) {
            redisHash = redisUtils.hmGet(sysUserId);
            account = (String) redisHash.get("account");
        }
        if (StringHelper.isEmpty(account)) {
            return false;
        } else if (!username.equals(account)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * ----------------------------- 分割线 以下是setter方法，用于替换field上的@Autowired -----------------------------
     **/

    @Autowired
    @Lazy
    public void setConstantQiniu(ConstantQiniu constantQiniu) {
        this.constantQiniu = constantQiniu;
    }

    @Autowired
    @Lazy
    public void setRedisUtils(RedisUtils redisUtils) {
        this.redisUtils = redisUtils;
    }
}
